*###########################################################*
* Effects of Settlement into Ethnic Enclaves on Immigrant   *
* Voter Turnout                                             *
* Andersson, Lajevardi, Lindgren, and Oskarsson             *
* Journal: Journal of Politics                              *
* Stata Replication Code                                    *
*###########################################################*

*******************************************************
* 2. CREATE ENCLAVE SIZES AND PREPARE SOME OTHER DATA *
******************************************************

*This do-file prepares some files used in the analysis
*most importantly the enclave files: share of coethnics in parish
*also: some info used on additional analysis: vote for radical right party 1991 (heterogeneity) and
*moving between municipalities (used as additional control)

///SET DIRECTORY/log-files/set more off

clear all
cd "D:\SCB_ConPol\Stata"
set more off

*capture log close
*log using "C:\Userdata\Shared\Logs\Ethnic Enclaves, political participation\log_enclaves", replace



///ENCLAVES MEASURES - SHARE OF COETHNICS IN PARISH

///small preperation of ses data that can be dropped at the end of the do-file
*creates the socioeconomic measures from 1985 that can be merged on to the 1987,1988,1999 cohorts
use "E:\ProjData\SESData\ses-SESData"
keep if Ar==1985
keep LopNr SamRakInk UtbNiva treiman
*4 duplicates; delete
duplicates drop LopNr, force
save "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure1987temp", replace
save "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure1988temp", replace
save "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure1989temp", replace
clear

///Create the socioeconomic measures from 1990 that can be merged on to the 1990,1991 cohorts 
use "E:\ProjData\SESData\ses-SESData"
keep if Ar==1990
keep LopNr SamRakInk UtbNiva treiman
*87 duplicates; delete
duplicates drop LopNr, force
save "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure1990temp", replace
save "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure1991temp", replace
clear


///NOW CREATE THE ENCLAVE MEASURES FOR EACH YEAR
foreach y in 87 88 89 90 91 {

///first open and prepare migration-files
*to pick out those who migrated year `y' is useful to know how many in the enclave that 
*were in the same cohort as the immigrants we are analysing, we are, however,
*currently not using this info

use "RTB\migrationer"

*Create immigration variable - keep immigrants

tab PostTyp, gen(m)
	keep if m1==1
	drop PostTyp m1 m2
 
*Generate year, month of arrival - keep only year in question 

tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace

gen im_month = substr(Datum, 5, 2) 
destring im_month, replace

	keep if im_year==19`y'

*small share who immigrated twise during a year
*Keep the observation from the latest immigration month

duplicates tag LopNr, gen(tag)
sort tag LopNr

bysort LopNr: egen latestmonth = max(im_month)
	drop if im_month!=latestmonth & tag>0
		*duplicates report LopNr - OK
	drop tag latestmonth

///HAVING PREPARED THOSE WHO IMMIGRATED IN `Y', WE MERGE WITH IND-DATA ON ALL WHO LIVE IN THE COUNTRY

*Use 1:m beacause there are some id-number duplicates in RTB-file
merge 1:m LopNr using "RTB\RTB_19`y'"
	drop if _merge==1 /*Not left in th country at the end of the year*/
	drop _merge

*Small number of duplicates remaining - throw out one obs.
duplicates drop LopNr, force

///MERGE WITH BIRTH COUNTRY DATA
merge 1:m LopNr using "RTB\FodelselandIFAUGrupp"
	drop _merge 
	keep if Kommun!=. /*Keep only residents in that year*/
	*duplicates report LopNr - OK

***Now create fill in the information on country of birth (labels)

lab def IFAUkod 0 "Sweden"
lab def IFAUkod 26 "Finland", add
lab def IFAUkod 27 "Denmark", add
lab def IFAUkod 28 "Norway & Iceland", add
lab def IFAUkod 29 "Bosnia", add
lab def IFAUkod 30 "Yugoslavia", add
lab def IFAUkod 31 "Poland", add
lab def IFAUkod 32 "UK & Ireland", add
lab def IFAUkod 33 "Germany", add
lab def IFAUkod 34 "Mediterranian", add
lab def IFAUkod 35 "Baltic", add
lab def IFAUkod 36 "East Europe", add
lab def IFAUkod 37 "Hungary & Czech & Slovakia", add
lab def IFAUkod 38 "Benelux & Swizz & Austria", add
lab def IFAUkod 39 "US & Canada", add
lab def IFAUkod 40 "Central America", add
lab def IFAUkod 41 "Chile", add
lab def IFAUkod 42 "South America", add
lab def IFAUkod 43 "East Africa", add
lab def IFAUkod 44 "Mena", add
lab def IFAUkod 45 "Other Africa", add
lab def IFAUkod 46 "Iran", add
lab def IFAUkod 47 "Iraq", add  
lab def IFAUkod 48 "Turkey", add
lab def IFAUkod 49 "East Asia", add
lab def IFAUkod 50 "South East Asia", add
lab def IFAUkod 51 "South Asia", add
lab def IFAUkod 52 "Oceania", add
lab def IFAUkod 53 "Unknown or Stateless", add

lab values IFAUkod IFAUkod	

tab IFAUkod, sort


///NOW WANT TO CREATE THE NUMBER OF COETHNICS ON PARISH LEVEL- 

*all coethnics
tab IFAUkod, gen(country)
foreach k of numlist 1(1)29 {
replace country`k' = . if country`k'==0
}

///those who came the same year
tab IFAUkod if im_year==19`y', gen(im)
*29 doesnt exist for 1987
capture gen im29=.
foreach l of numlist 1(1)29 {
replace im`l' = . if im`l'==0
}

///population
gen pop =1 

*Number of foreign born generally
gen foreign = 1 if IFAUkod!=0


///politically integrated  - share who voted in election in 1982

merge 1:m LopNr using "VD/Valdelt_82"
drop _merge

duplicates drop LopNr, force 

*variable for voted in municipal election 1982
gen votedk = 1 if k==2 | k==4 | k==5 | k==6

*right to vote
gen rightk = 1 if k==2 | k==1 | k==4 | k==5 | k==6

*create the immigration variables to collapse
tab IFAUkod if votedk==1, gen(voted)
foreach h of numlist 1(1)29 {
replace voted`h' = . if voted`h'==0
}

tab IFAUkod if rightk==1, gen(right)
foreach a of numlist 1(1)29 {
replace right`a' = . if right`a'==0
}


///economically integrated  - ses measures 

merge 1:1 LopNr using "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure19`y'temp"
drop if _merge==2 /*drop those not in the main data*/
drop _merge

///treiman
foreach b of numlist 1(1)29 {
gen treiman`b' = treiman if country`b'==1
}

///total income

foreach g of numlist 1(1)29 {
gen inc`g' = SamRakInk if country`g'==1
}

///Education
gen high_educ = 1 if UtbNiva>3 
replace high_educ=. if UtbNiva==.

tab IFAUkod if high_educ==1, gen(educ)
foreach r of numlist 1(1)29 {
replace educ`r' = . if educ`r'==0
}

collapse (count) pop foreign votedk rightk im1 im2 im3 im4 im5 im6 im7 im8 im9 im10 im11 im12 im13 im14 ///
im15 im16 im17 im18 im19 im20 im21 im22 im23 im24 im25 im26 im27 im28 im29 /// 
country1 country2 country3 country4 country5 country6 country7 country8 country9 ///
country10 country11 country12 country13 country14 country15 country16 country17 /// 
country18 country19 country20 country21 country22 country23 country24 country25 ///
country26 country27 country28 country29 voted1 voted2 voted3 voted4 voted5 voted6 ///
voted7 voted8 voted9 voted10 voted11 voted12 voted13 voted14 voted15 voted16 /// 
voted17 voted18 voted19 voted20 voted21 voted22 voted23 voted24 voted25 voted26 /// 
voted27 voted28 voted29 right1 right2 right3 right4 right5 right6 right7 right8 ///
right9 right10 right11 right12 right13 right14 right15 right16 right17 right18 /// 
right19 right20 right21 right22 right23 right24 right25 right26 right27 right28 ///
right29 educ1 educ2 educ3 educ4 educ5 educ6 educ7 educ8 ///
educ9 educ10 educ11 educ12 educ13 educ14 educ15 educ16 educ17 educ18 /// 
educ19 educ20 educ21 educ22 educ23 educ24 educ25 educ26 educ27 educ28 ///
educ29 (mean) treiman1 treiman2 treiman3 treiman4 treiman5 treiman6 treiman7 /// 
treiman8 treiman9 treiman10 treiman11 treiman12 treiman13 treiman14 treiman15 /// 
treiman16 treiman17 treiman18 treiman19 treiman20 treiman21 treiman22 treiman23 /// 
treiman24 treiman25 treiman26 treiman27 treiman28 treiman29 inc1 inc2 inc3 inc4 inc5 inc6 inc7 inc8 inc9 inc10 inc11 inc12 inc13 inc14 ///
inc15 inc16 inc17 inc18 inc19 inc20 inc21 inc22 inc23 inc24 inc25 inc26 inc27 inc28 inc29, by(Forsamling)

save "E:\ProjData\Ethnic Enclaves, political participation\temp\enclave_parish`y'", replace

}

foreach y in 87 88 89 90 91 {
erase "E:\ProjData\Ethnic Enclaves, political participation\temp\ses-measure19`y'temp.dta"
}





///ALSO PREPARE DATA FOR RADICAL RIGHT PARTY IN 1991
///ONLY USED FOR A HETEROGENEITY CHECK IN THE APPENDIX LATER ON

///DATA-FILE BELOW HAS NUMBER OF VOTES FOR ALL PARTIES PER ELECTION DISTRICT IN 1991
use "D:\Data\ExtData\ValResData\Riksdagsvalen1973_2006\0237_rv91.dta"

*keep only the votes for the radical right party "ny demokrati", and information to 
*create an average voting percentage on municipality level
keep v13 v7 v16 v26

*district (id-variable) and Municipality
rename v13 district
rename v7 muni
*Number of votes
rename v16 Votes
*Votes for "ny demokrati"
rename v26 Votes_nd

///Now collapse; to get votes on municipality level
collapse (sum) Votes Votes_nd, by(muni)

/// gen percentage vote for ND
gen votes_nd = Votes_nd/Votes

///for merging puposes replace "muni" as standard integer with different name
gen Kommun = muni
///drop non-essential variables
drop muni Votes Votes_nd

save "E:\ProjData\Ethnic Enclaves, political participation\temp\nd91", replace





///LAST: ALSO PREPARE CONTROL FOR MOVING FIRST YEAR
///we add a control for if a person moves or not in arrival year
foreach y in 87 88 89 90 91 {

*open data file with info on all migrations over a large period of time 
use "RTB\migrationer"

*Create immigration variable - keep immigrants - drop emmigrations

tab PostTyp, gen(m)
	keep if m1==1
	drop PostTyp m1 m2
 
*Generate year and month of arrival - 

tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace

gen im_month = substr(Datum, 5, 2) 
destring im_month, replace

*keep only year in question 

keep if im_year==19`y'

*small share who immigrated twise during a year - only need one obs. 

duplicates tag LopNr, gen(tag)
sort tag LopNr

*Keep the observation from the latest immigration month
bysort LopNr: egen latestmonth = max(im_month)
	drop if im_month!=latestmonth & tag>0
*check for duplicates - OK
	duplicates report LopNr
	drop tag latestmonth

///NOW MERGE DATA ON MOVING - includes all moves over an extended period of time
merge 1:m LopNr using "RTB/Inrikesflyttar", keepusing(LopNr FryttDat Kommun KommunG)

drop if _merge==2
drop _merge

*Variable for movingdates
tostring FryttDat, replace
gen move_year = substr(FryttDat, 1, 4) 
destring move_year, replace

gen move_month = substr(FryttDat, 5, 2) 
destring move_month, replace

gen move_day = substr(FryttDat, 7, 2) 
destring move_day, replace

sort LopNr move_year move_month move_day 

*Only interested in moves in year`y'

keep if im_year==move_year

*Moved out of municipality if municiplaities are different
gen move_out_tmp = 1 if Kommun!=KommunG
gen move=1 if Kommun==KommunG

bysort LopNr: egen move_out_muni = max(move_out_tmp)

duplicates drop LopNr, force


keep LopNr move_out_muni move
save  "E:\ProjData\Ethnic Enclaves, political participation\temp\movers_`y'", replace

}

*log close


